cmake_minimum_required(VERSION 3.0.2)

project(media-downloader LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set( PGR_VERSION "2.8.0" )
set( COPYRIGHT_YEARS "2021-2023" )

INCLUDE(GNUInstallDirs)

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    add_definitions(-Qunused-arguments)
endif()

if(${CMAKE_VERSION} VERSION_LESS "3.1.0")
    message("Warning your cmake version is too old(<3.1.0) and code my not compile")
    add_definitions(-std=c++14)
endif()

find_package(Qt5 COMPONENTS Core Widgets Network REQUIRED)

include_directories(${PROJECT_BINARY_DIR})

# uninstall target
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
    "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
    IMMEDIATE @ONLY)

add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake)

set(UI_FILES
    src/mainwindow.ui
    src/logwindow.ui)

if(Qt5Core_VERSION VERSION_LESS 5.4.0)
    set(MOC_FILES
        src/mainwindow.h
        src/logwindow.h
        src/basicdownloader.h
        src/batchdownloader.h
        src/configure.h
        src/library.h
        src/playlistdownloader.h
        src/utils/single_instance.hpp)
else()
    set(MOC_FILES
        src/mainwindow.h
        src/logwindow.h
        src/basicdownloader.h
        src/batchdownloader.h
        src/configure.h
        src/library.h
        src/playlistdownloader.h)
endif()

set(SRC
    src/mainwindow.cpp
    src/logwindow.cpp
    src/main.cpp
    src/settings.cpp
    src/utility.cpp
    src/basicdownloader.cpp
    src/about.cpp
    src/themes.cpp
    src/batchdownloader.cpp
    src/tabmanager.cpp
    src/directoryEntries.cpp
    src/configure.cpp
    src/versionInfo.cpp
    src/translator.cpp
    src/playlistdownloader.cpp
    src/networkAccess.cpp
    src/logger.cpp
    src/engines.cpp
    src/tableWidget.cpp
    src/library.cpp
    src/engines/tests.cpp
    src/engines/yt-dlp.cpp
    src/engines/safaribooks.cpp
    src/engines/generic.cpp
    src/engines/aria2c.cpp
    src/engines/lux.cpp
    src/engines/wget.cpp
    src/engines/svtplay-dl.cpp
    src/engines/you-get.cpp
    src/engines/gallery-dl.cpp)

set(RESOURCES src/icon.qrc )

if(WIN32)
    set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/src/appicon.rc")
    add_executable(media-downloader WIN32 ${SRC} ${MOC_FILES} ${UI_FILES} ${RESOURCES} ${APP_ICON_RESOURCE_WINDOWS})
    set_target_properties(media-downloader PROPERTIES COMPILE_FLAGS "-Wextra -Wall -s -pedantic")
else()
    add_executable(media-downloader ${SRC} ${MOC_FILES} ${UI_FILES} ${RESOURCES})
    set_target_properties(media-downloader PROPERTIES COMPILE_FLAGS "-Wextra -Wall -s -fPIC -pedantic")
endif()

target_link_libraries(media-downloader Qt5::Widgets Qt5::Core Qt5::Network)

install(TARGETS media-downloader RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

install(FILES src/media-downloader.desktop DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/applications)
install(FILES src/media-downloader.png DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pixmaps)

install(DIRECTORY translations DESTINATION ${CMAKE_INSTALL_DATADIR}/media-downloader FILES_MATCHING PATTERN "*.qm")

file(WRITE ${PROJECT_BINARY_DIR}/locale_path.h "\n#define TRANSLATION_PATH \"${CMAKE_INSTALL_FULL_DATADIR}/media-downloader/translations/\"\n")

file(WRITE ${PROJECT_BINARY_DIR}/version.h
"
#define VERSION \"${PGR_VERSION}\"
#define QTVERSION \"${Qt5Core_VERSION}\"
#define COPYRIGHT \"${COPYRIGHT_YEARS}\"
")

if(WIN32)
    find_path(libstdc_PATH libstdc++-6.dll)
    find_path(gcc_PATH gcc.exe)
    find_path(windeployqt_PATH windeployqt.exe PATHS "${CMAKE_PREFIX_PATH}/bin")

    file(WRITE ${PROJECT_BINARY_DIR}/media-downloader_installer_commands.txt
"
set PATH=${gcc_PATH};${libstdc_PATH};%PATH%\r\n
${windeployqt_PATH}/windeployqt --release media-downloader.exe\r\n
")

file(WRITE ${PROJECT_BINARY_DIR}/media-downloader_windows_installer.iss
"
; Script generated by the Inno Setup Script Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!

#define MyAppName            \"Media Downloader\"
#define MyAppVersion         \"${PGR_VERSION}\"
#define MyAppPublisher       \"Francis Banyikwa\"
#define MyAppURL             \"https://github.com/mhogomchungu/media-downloader\"
#define MyAppExeName         \"media-downloader.exe\"
#define MyAppCopyright       \"${COPYRIGHT_YEARS}, Francis Banyikwa\"
#define MyLibrariesLocation  \"C:\\projects\\media-downloader.libraries.Qt-${Qt5Core_VERSION}\\\"
#define MyEXELocation        \"${PROJECT_BINARY_DIR}\\media-downloader.exe\"
#define MyBuildPath          \"${PROJECT_BINARY_DIR}\"
#define MyOutPutPath         \"C:\\projects\\media-downloader.installer\\Qt-${Qt5Core_VERSION}\"
#define MySourcesPath        \"C:\\projects\\media-downloader\\\"

[Setup]
AppId={{6DD595EF-ECA2-481B-B008-CB7302603A0D}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppCopyright={#MyAppCopyright}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
VersionInfoDescription={#MyAppName} installer
VersionInfoProductName={#MyAppName} {#MyAppVersion}
VersionInfoVersion={#MyAppVersion}.0
UninstallDisplayName={#MyAppName}
UninstallDisplayIcon={#MySourcesPath}\\src\\media-downloader.ico

DefaultDirName={pf}\\{#MyAppName}
LicenseFile={#MySourcesPath}\\LICENSE.txt
OutputDir={#MyOutPutPath}
SetupIconFile={#MySourcesPath}\\src\\media-downloader.ico
Compression=lzma
SolidCompression=yes
DisableProgramGroupPage=yes
OutputBaseFilename=MediaDownloader-${PGR_VERSION}.setup

[Tasks]
Name: \"desktopicon\"; Description: \"{cm:CreateDesktopIcon}\"; GroupDescription: \"{cm:AdditionalIcons}\"; Flags: unchecked
Name: \"quicklaunchicon\"; Description: \"{cm:CreateQuickLaunchIcon}\"; GroupDescription: \"{cm:AdditionalIcons}\"; Flags: unchecked; OnlyBelowVersion: 0,6.1

[Files]
Source: {#MyEXELocation}; DestDir: {app}; Flags: ignoreversion
Source: {#MyLibrariesLocation}\\*.dll; DestDir: {app} ; Flags: ignoreversion
Source: {#MySourcesPath}\\LICENSE.txt; DestDir: {app}; Flags: ignoreversion
Source: {#MyLibrariesLocation}\\bearer\\* ; DestDir: {app}\\bearer; Flags: ignoreversion
Source: {#MyLibrariesLocation}\\iconengines\\* ; DestDir: {app}\\iconengines; Flags: ignoreversion
Source: {#MyLibrariesLocation}\\imageformats\\*; DestDir: {app}\\imageformats; Flags: ignoreversion
Source: {#MyLibrariesLocation}\\styles\\*; DestDir: {app}\\styles; Flags: ignoreversion
Source: {#MyLibrariesLocation}\\platforms\\*; DestDir: {app}\\platforms; Flags: ignoreversion
Source: {#MyLibrariesLocation}\\3rdParty\\*; DestDir: {app}\\3rdParty; Flags: ignoreversion recursesubdirs
Source: {#MySourcesPath}\\translations\\*; DestDir: {app}\\translations; Flags: ignoreversion
Source: {#MySourcesPath}\\src\\media-downloader.ico; DestDir: {app}\\icons; Flags: ignoreversion

; NOTE: Don't use \"Flags: ignoreversion\" on any shared system files

[Icons]
Name: \"{commonprograms}\\{#MyAppName}\"; Filename: \"{app}\\{#MyAppExeName}\"; IconFilename: \"{app}\\icons\\media-downloader.ico\"; WorkingDir: \"{app}\"
;Name: \"{commonprograms}\\Uninstall {#MyAppName}\"; Filename: \"{uninstallexe}\"; IconFilename: \"{app}\\icons\\media-downloader.ico\"; WorkingDir: \"{app}\"
Name: \"{commondesktop}\\{#MyAppName}\"; Filename: \"{app}\\{#MyAppExeName}\"; IconFilename: \"{app}\\icons\\media-downloader.ico\"; WorkingDir: \"{app}\"; Tasks: desktopicon
Name: \"{userappdata}\\Microsoft\\Internet Explorer\\Quick Launch\\{#MyAppName}\"; Filename: \"{app}\\{#MyAppExeName}\"; IconFilename: \"{app}\\icons\\media-downloade.ico\"; WorkingDir: \"{app}\"; Tasks: quicklaunchicon

[Run]
Filename: \"{app}\\{#MyAppExeName}\"; Description: \"{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}\"; Flags: nowait postinstall skipifsilent

[Languages]
Name: \"english\"             ; MessagesFile: \"compiler:Default.isl\"
Name: \"brazilianportuguese\" ; MessagesFile: \"compiler:Languages\\BrazilianPortuguese.isl\"
Name: \"catalan\"             ; MessagesFile: \"compiler:Languages\\Catalan.isl\"
Name: \"corsican\"            ; MessagesFile: \"compiler:Languages\\Corsican.isl\"
Name: \"czech\"               ; MessagesFile: \"compiler:Languages\\Czech.isl\"
Name: \"danish\"              ; MessagesFile: \"compiler:Languages\\Danish.isl\"
Name: \"dutch\"               ; MessagesFile: \"compiler:Languages\\Dutch.isl\"
Name: \"finnish\"             ; MessagesFile: \"compiler:Languages\\Finnish.isl\"
Name: \"french\"              ; MessagesFile: \"compiler:Languages\\French.isl\"
Name: \"german\"              ; MessagesFile: \"compiler:Languages\\German.isl\"
Name: \"hebrew\"              ; MessagesFile: \"compiler:Languages\\Hebrew.isl\"
Name: \"italian\"             ; MessagesFile: \"compiler:Languages\\Italian.isl\"
Name: \"japanese\"            ; MessagesFile: \"compiler:Languages\\Japanese.isl\"
Name: \"norwegian\"           ; MessagesFile: \"compiler:Languages\\Norwegian.isl\"
Name: \"polish\"              ; MessagesFile: \"compiler:Languages\\Polish.isl\"
Name: \"portuguese\"          ; MessagesFile: \"compiler:Languages\\Portuguese.isl\"
Name: \"russian\"             ; MessagesFile: \"compiler:Languages\\Russian.isl\"
Name: \"slovenian\"           ; MessagesFile: \"compiler:Languages\\Slovenian.isl\"
Name: \"spanish\"             ; MessagesFile: \"compiler:Languages\\Spanish.isl\"
Name: \"turkish\"             ; MessagesFile: \"compiler:Languages\\Turkish.isl\"
Name: \"ukrainian\"           ; MessagesFile: \"compiler:Languages\\Ukrainian.isl\"
")

endif()
